home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 …ember: Reference Library / Dev.CD Dec 96 RL / Dev.CD Dec 96 RL.toast / What's New? / Sample Code / Snippets / OS Utilities / DeskPat / deskpat.c next >
Encoding:
C/C++ Source or Header  |  1996-10-11  |  3.9 KB  |  203 lines  |  [TEXT/CWIE]

  1. /*
  2.     deskpat.c
  3.  
  4.     Getting Desktop and Utilities Pattern  snippet
  5.     Albert Hui -- MacDTS
  6.     
  7.     This snippet how you can  access the "Desktop Pattern" and 
  8.     the "Set Utilities Pattern" pattern.(This pattern is set by holding down \
  9.     the option key in the Desktop Patterns Control Panel.)
  10.     
  11.     This control panel uses resources of type 'ppat' to store 
  12.     this pattern. The `ppat' resource is stored in the System file
  13.     in your System Folder; the desktop pattern has an ID of 16 and 
  14.     the utilities pattern has an ID of 42. Since this is not
  15.     documented, it could be subject to change at any moment. 
  16.     You should be careful when using this.
  17.  
  18.     This sample is built with CW 9. 
  19.  
  20.  
  21. */
  22.  
  23.  
  24. #include "main.h"
  25.  
  26. void MainLoop(void);
  27. void HandleKeyDown (EventRecord *);
  28. void MacInits(void);
  29. void FinishUp(void);
  30. void HandleUpdates(EventRecord *);
  31. void HandleMouseDown(EventRecord *);
  32.  
  33. Boolean   gDone = false;
  34. Boolean   gInBackground = false;
  35. WindowPtr gWindow1 = NULL;
  36. WindowPtr gWindow2 = NULL;
  37. static  PixPatHandle ppatHandle;
  38.  
  39.  
  40. void main()
  41. {
  42.   MacInits();
  43.  
  44.   gWindow1    = GetNewCWindow (128, NULL, (WindowPtr) -1);
  45.   gWindow2 = GetNewCWindow (129, NULL, (WindowPtr) -1);
  46.   SelectWindow(gWindow1);
  47.   SetPort (gWindow1);
  48.  
  49.   MainLoop();
  50.   FinishUp();
  51. }
  52.  
  53. void MacInits()
  54. {
  55.   MaxApplZone();
  56.   MoreMasters();
  57.   MoreMasters();
  58.  
  59.   InitGraf(&qd.thePort);
  60.   InitFonts(); 
  61.   InitWindows();
  62.   InitMenus();
  63.   TEInit();                               
  64.   InitDialogs(0L);                        
  65.   FlushEvents (everyEvent, 0);          
  66.   InitCursor(); 
  67. }
  68.  
  69.  
  70. void MainLoop(void)
  71. {
  72.   EventRecord    theEvent;
  73.  
  74.   while(!gDone) {
  75.     if (WaitNextEvent (everyEvent, &theEvent, 30, 0L)) {
  76.         switch (theEvent.what) {
  77.         case mouseDown:
  78.           HandleMouseDown(&theEvent);
  79.           break;
  80.  
  81.         case autoKey:
  82.         case keyDown:
  83.           HandleKeyDown(&theEvent);
  84.           break;
  85.  
  86.         case updateEvt:
  87.           HandleUpdates(&theEvent);
  88.           break;
  89.  
  90.         case activateEvt:
  91.           gInBackground = false;
  92.           break;
  93.       }
  94.     } 
  95.   }
  96. }
  97.  
  98.  
  99.  
  100. void HandleKeyDown (EventRecord *evt)
  101. {
  102.   char  theChar;
  103.  
  104.   theChar = (evt->message & charCodeMask);
  105.  
  106.   if ((evt->modifiers & cmdKey) != 0) {
  107.     switch (theChar) {
  108.       case 'q': case 'Q':
  109.         gDone = true;
  110.         break;
  111.     }
  112.   }
  113. }
  114.  
  115.  
  116. void HandleMouseDown(EventRecord *theEvent)
  117. {
  118.   WindowPtr     whichWindow;
  119.   short         thePart, controlCode;
  120.   short         x, y;
  121.   ControlHandle   whichControl;
  122.   Point         myPoint;
  123.  
  124.   thePart = FindWindow (theEvent->where, &whichWindow);
  125.  
  126.   switch (thePart) {
  127.     case inSysWindow:
  128.       SystemClick (theEvent, whichWindow);
  129.       break;
  130.  
  131.     case inDrag:
  132.       DragWindow(whichWindow, theEvent->where, &qd.screenBits.bounds);
  133.       break;
  134.  
  135.     case inContent:
  136.       SetPort (whichWindow);
  137.       if (whichWindow != FrontWindow())
  138.         SelectWindow(whichWindow);
  139.       break;
  140.  
  141.     case inGoAway:
  142.       if (whichWindow == gWindow1 &&
  143.          TrackGoAway(gWindow1, theEvent->where) )
  144.         HideWindow(gWindow1);
  145.       gDone = true;
  146.       break;
  147.   }
  148. }
  149.  
  150. void HandleUpdates(EventRecord *theEventPtr)
  151. {
  152.   GrafPtr savedPort;
  153.   WindowPtr windowPtr;
  154.   Rect destRect;
  155.  
  156.   GetPort(&savedPort);
  157.  
  158.   windowPtr = (WindowPtr) theEventPtr->message;
  159.  
  160.   BeginUpdate(windowPtr);
  161.  
  162.   if(!EmptyRgn(windowPtr->visRgn))
  163.   {
  164.     SetPort(windowPtr);
  165.  
  166.     if (windowPtr == gWindow2) {
  167.       ppatHandle = (PixPatHandle) GetPixPat(16);
  168.       if (ppatHandle != NULL) {
  169.           SetRect(&destRect, 15, 125, 197, 164);
  170.           FrameRect(&destRect);
  171.           FillCRect(&destRect, ppatHandle);
  172.           DisposePixPat(ppatHandle);
  173.       }
  174.      }
  175.     if (windowPtr == gWindow1) {
  176.       ppatHandle = (PixPatHandle) GetPixPat(42);
  177.       if (ppatHandle != NULL) {
  178.           SetRect(&destRect, 15, 125, 197, 164);
  179.           FrameRect(&destRect);
  180.           FillCRect(&destRect, ppatHandle);
  181.           DisposePixPat(ppatHandle);
  182.       }
  183.      }
  184.   }
  185.  
  186.   EndUpdate(windowPtr);
  187.   SetPort(savedPort);
  188. }
  189.  
  190.  
  191.  
  192. void FinishUp()
  193. {
  194.   if (gWindow2)
  195.     DisposeWindow(gWindow2);
  196.  
  197.   if (gWindow1)
  198.     DisposeWindow(gWindow1);
  199.  
  200.   ExitToShell();
  201. }
  202.  
  203.